﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System.Linq;
using System.Xml.Linq;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntryDataAccess
{
    public class RegExFormatAndSample
    {
        public string Format { get; set; }
        public string Example { get; set; }
    }


    public static class LookupRegularExpression
    {
        private readonly static string strFormatFileName = "RegExpressions.xml";


        public static RegExFormatAndSample GetFormatAndExample( string strFormatId )
        {
            XElement data = XElement.Load( strFormatFileName );

            var query =
                 from item in data.Descendants( "RegEx" )
                 where (string)item.Attribute( "Id" ) == strFormatId
                 select new RegExFormatAndSample
                 {
                     Format = item.Element( "Pattern" ).Value.Trim(),
                     Example = item.Element( "Example" ).Value.Trim()
                 };

            if( query.Count() > 0 )
                return query.ElementAt( 0 );

            return null;
        }
    }
}
